Imports System.Diagnostics
Imports System.Windows.Forms

Public Class MainForm


    Friend presenter As FileManagerViewPresenter

    Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        presenter = New FileManagerViewPresenter(Me, Me.MainFileManagerView)

    End Sub


    Private Sub VersionButton_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click

        Me.presenter.ShowVersion()


    End Sub

    Private Sub PortalsButton_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles ListPortalsToolStripMenuItem.Click

        Me.presenter.ListPortals()

    End Sub


    'Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    '    Handles DeleteToolStripMenuItem.Click

    '    Me.presenter.BeginDeleteSelectedFiles()

    'End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

        Me.Close()

    End Sub

    Private Sub ToolBarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolBarToolStripMenuItem.Click

        ToolBarToolStripMenuItem.Checked = Not ToolBarToolStripMenuItem.Checked
        ToolStrip.Visible = ToolBarToolStripMenuItem.Checked

    End Sub

    Private Sub StatusBarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StatusBarToolStripMenuItem.Click

        StatusBarToolStripMenuItem.Checked = Not StatusBarToolStripMenuItem.Checked
        StatusStrip.Visible = StatusBarToolStripMenuItem.Checked

    End Sub

    Private Sub FoldersToolStripMenuItem_Click _
        (ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles FoldersToolStripMenuItem.Click, FoldersToolStripButton.Click

        Me.presenter.ToggleFoldersVisible()

    End Sub

    Private Sub FileTransfersToolStripMenuItem_Click _
        (ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles FileTransfersToolStripMenuItem.Click, FileTransfersToolStripButton.Click

        Me.presenter.ToggleFileTransfersVisible()


    End Sub

    Private Sub ListToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
     Handles ListToolStripMenuItem.Click

        Me.presenter.SetView(View.List)

    End Sub

    Private Sub DetailsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
     Handles DetailsToolStripMenuItem.Click

        Me.presenter.SetView(View.Details)

    End Sub

    Private Sub LargeIconsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
     Handles LargeIconsToolStripMenuItem.Click

        Me.presenter.SetView(View.LargeIcon)

    End Sub

    Private Sub SmallIconsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
     Handles SmallIconsToolStripMenuItem.Click

        Me.presenter.SetView(View.SmallIcon)

    End Sub

    Private Sub TileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
     Handles TileToolStripMenuItem.Click

        Me.presenter.SetView(View.Tile)

    End Sub


    Private Sub UploadFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UploadFileToolStripMenuItem.Click

        Me.MainFileManagerView.UploadFile_Click(sender, e)

    End Sub

    Private Sub Login()

        If Not Me.ConnectionBackgroundWorker.IsBusy Then

            If My.Forms.LoginForm.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then

                Me.ToolStripStatusLabel.Text = "Connecting..."

                Me.ConnectionBackgroundWorker.RunWorkerAsync()

            End If

        End If
    End Sub

    Private Sub WebServiceConnectionToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WebServiceConnectionToolStripMenuItem.Click

        Me.Login()

    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click

        Me.MainFileManagerView.DownloadFiles_Click(sender, e)

    End Sub


    Private Sub ConnectionBackgroundWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles ConnectionBackgroundWorker.DoWork

        e.Result = String.Empty

        Me.presenter.SetConnection(e.Result)

    End Sub

    Private Sub ConnectionBackgroundWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles ConnectionBackgroundWorker.RunWorkerCompleted

        Me.ToolStripStatusLabel.Text = e.Result.ToString

        Try

            Me.presenter.PopulateRootFolders()

            Me.presenter.BeginGetFilesByFolder(-1)

        Catch ex As Exception

            MessageBox.Show("Could not retrieve a list of files and folders.", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)


        End Try


    End Sub

    Private Sub MainForm_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        Me.Login()

    End Sub


    Private Sub StatusWindowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StatusWindowToolStripMenuItem.Click

        My.Forms.StatusDialog.Show()

    End Sub



End Class
